package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.config.JacksonObjectMapper;
import com.itheima.dto.LoginDTO;
import com.itheima.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/member")
@Slf4j
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 手机验证码登录
     * @param loginDTO 手机验证码登录
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginDTO loginDTO){
        JacksonObjectMapper mapper = new JacksonObjectMapper();
        try {
            log.info("手机验证码登录, {}", mapper.writeValueAsString(loginDTO));
            Result result = memberService.login(loginDTO);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "未知错误，请联系管理员", null);
        }
    }
}
